package com.cyt.web.controller.sys;

import com.cyt.base.enums.BusinessType;
import com.cyt.base.enums.OperatorType;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.sys.aspect.BaseLog;
import com.cyt.sys.model.po.SysRule;
import com.cyt.sys.model.vo.SysRuleVo;
import com.cyt.sys.service.SysRuleService;
import com.github.pagehelper.StringUtil;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;


/**
 * 数据权限Controller实现类
 *
 * @author shengQiang yu
 * @date 2019-03-22 10:04
 */
@RestController
public class SysRuleController extends SysBaseController {

    @Resource
    private SysRuleService sysRuleService;


    /**
     * 系统数据权限统一跳转处理
     *
     * @param model 对象
     * @return
     * @author shengQiang yu
     * @date 2019-03-22 10:18
     */
    @RequestMapping("/sys/rule/*")
    public Model goPage(Model model) {
        return model;
    }

    /**
     * 获取数据权限集合
     *
     * @param sysRuleVo Vo对象
     * @return
     * @author shengQiang yu
     * @date 2019-03-22 10:37
     */
    @BaseLog(module = "系统模块", classz = "数据管理", methods = "获取数据权限集合", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sysRule/list", method = RequestMethod.POST)
    public ReturnResult list(SysRuleVo sysRuleVo) {
        ReturnResult returnResult = sysRuleService.list(sysRuleVo);
        return returnResult;
    }

    /**
     * 跳转到添加或修改页面
     *
     * @param id       主键id
     * @param parentId 父类id
     * @return
     * @author shengQiang yu
     * @date 2019-03-22 11:06
     */
    @RequestMapping(value = "/sysRule/getAddOrEdit", method = RequestMethod.GET)
    public ModelAndView getAddOrEdit(String id, String parentId) {
        ModelAndView view = new ModelAndView("/sys/rule/ruleEdit");
        SysRule sysRule = null;
        if (id != null && !"".equals(id)) {
            sysRule = sysRuleService.selectByPrimaryKey(id);
            view.addObject(sysRule);
        }
        if (StringUtil.isNotEmpty(parentId)) {
            SysRule sysRuleParent = sysRuleService.selectByPrimaryKey(parentId);
            if (sysRule == null) {
                sysRule = new SysRule();
            }
            sysRule.setParentName(sysRuleParent.getName());
            sysRule.setParentId(parentId);
            view.addObject(sysRule);
        }
        //view.addObject(sysRule);
        return view;
    }

    /**
     * 添加或修改
     *
     * @param sysRuleVo Vo对象
     * @return
     * @author shengQiang yu
     * @date 2019-03-22 11:45
     */
    @BaseLog(module = "系统模块", classz = "数据管理", methods = "添加或修改", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sysRule/addOrUpdate", method = RequestMethod.POST)
    public ReturnResult addOrUpdate(SysRuleVo sysRuleVo) {
        ReturnResult baseRule = sysRuleService.addOrUpdate(sysRuleVo);
        return baseRule;
    }

    /**
     * 获取数据权限树
     *
     * @param sysRuleVo vo对象
     * @return
     * @author shengQiang yu
     * @date 2019-03-22 14:57
     */
    @BaseLog(module = "系统模块", classz = "数据管理", methods = "数据权限树", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sysRule/dTreeList", method = RequestMethod.POST)
    public ReturnResult dTreeList(SysRuleVo sysRuleVo) {
        return sysRuleService.dTreeList(sysRuleVo);
    }

    /**
     * 获取下拉框数据
     *
     * @param sysRuleVo vo对象
     * @param dictType  字典码标识
     * @return
     * @author shengQiang yu
     * @date 2019-03-22 14:57
     */
    @BaseLog(module = "系统模块", classz = "数据管理", methods = " 获取下拉框数据", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/sysRule/selectData")
    public ReturnResult selectData(SysRuleVo sysRuleVo, String dictType) {
        return sysRuleService.selectData(sysRuleVo, dictType);
    }
}
